(function(r) {
  r(CodeMirror);
})(function(r) {
  var Y = "CodeMirror-hint", O = "CodeMirror-hint-active";
  r.showHint = function(t, e, i) {
    if (!e)
      return t.showHint(i);
    i && i.async && (e.async = !0);
    var n = { hint: e };
    if (i)
      for (var o in i)
        n[o] = i[o];
    return t.showHint(n);
  }, r.defineExtension("showHint", function(t) {
    t = Q(this, this.getCursor("start"), t);
    var e = this.listSelections();
    if (!(e.length > 1)) {
      if (this.somethingSelected()) {
        if (!t.hint.supportsSelection)
          return;
        for (var i = 0; i < e.length; i++)
          if (e[i].head.line != e[i].anchor.line)
            return;
      }
      this.state.completionActive && this.state.completionActive.close();
      var n = this.state.completionActive = new I(this, t);
      n.options.hint && (r.signal(this, "startCompletion", this), n.update(!0));
    }
  }), r.defineExtension("closeHint", function() {
    this.state.completionActive && this.state.completionActive.close();
  });
  function I(t, e) {
    if (this.cm = t, this.options = e, this.widget = null, this.debounce = 0, this.tick = 0, this.startPos = this.cm.getCursor("start"), this.startLen = this.cm.getLine(this.startPos.line).length - this.cm.getSelection().length, this.options.updateOnCursorActivity) {
      var i = this;
      t.on("cursorActivity", this.activityFunc = function() {
        i.cursorActivity();
      });
    }
  }
  function X(t, e) {
    var i = t.doc.modeOption;
    if (i.name === "formEvent") {
      var n = /[0-9a-zA-Z]+\(\s*[0-9a-zA-Z,/"]+\s*\)/;
      if (n.test(e)) {
        var o = t.getCursor(), l = t.getRange(r.Pos(o.line, 0), t.getCursor("start")), s = l.split("").reverse().join("").split(".")[0].split("").reverse().join("").replace("(", ",").replace(")", "");
        s != null && s != "" && (s = "." + s);
        for (var u = l.substring(0, l.length - s.length - 1).replace(/\(/g, ",").replace(/,/g, ")"), f = u + s, a = f.split(","), c = a[0].length + 2, h = [], p = 1; p < a.length; p++) {
          var g = c + a[p].length - 2;
          a[p].charAt(0) != '"' && (c = a[0].length + 1, g = c + a[p].length);
          var v = t.newRange(r.Pos(o.line, c), r.Pos(o.line, g));
          h.push(v), p === 1 && t.setSelection(r.Pos(o.line, c), r.Pos(o.line, g), { scroll: !1 }), c = c + a[p].length;
        }
        var w = { ranges: h, primary: 1 };
        t.setOption("selection", w);
      }
    } else {
      var n = /@[0-9a-zA-Z]+\([0-9a-zA-Z,@\*]+\)/, l = t.getRange(r.Pos(0, 0), t.getCursor("start"));
      if (n.test(e)) {
        var s = l.split("").reverse().join("").split("@")[0].split("").reverse().join("").replace("(", ",").replace(")", "");
        s != null && s != "" && (s = "@" + s);
        for (var u = l.substring(0, l.length - s.length - 1).replace(/\(/g, ",").replace(/,/g, ")"), f = u + s, a = f.split(","), c = a[0].length + 1, h = [], p = 1; p < a.length; p++) {
          var g = c + a[p].length, v = t.newRange(r.Pos(0, c), r.Pos(0, g));
          h.push(v), p === 1 && t.setSelection(r.Pos(0, c), r.Pos(0, g), { scroll: !1 }), c = g + 1;
        }
        var w = { ranges: h, primary: 1 };
        t.setOption("selection", w);
      }
    }
  }
  function R(t) {
    return typeof t == "string" ? t : t.key ? t.key : t.text;
  }
  var G = window.requestAnimationFrame || function(t) {
    return setTimeout(t, 1e3 / 60);
  }, J = window.cancelAnimationFrame || clearTimeout;
  I.prototype = {
    close: function() {
      this.active() && (this.cm.state.completionActive = null, this.tick = null, this.options.updateOnCursorActivity && this.cm.off("cursorActivity", this.activityFunc), this.widget && this.data && r.signal(this.data, "close"), this.widget && this.widget.close(), r.signal(this.cm, "endCompletion", this.cm));
    },
    active: function() {
      return this.cm.state.completionActive == this;
    },
    pick: function(t, e) {
      var i = t.list[e], n = this;
      this.cm.operation(function() {
        i.hint ? i.hint(n.cm, t, i) : n.cm.replaceRange(
          R(i),
          i.from || t.from,
          i.to || t.to,
          "complete"
        ), r.signal(t, "pick", i), n.cm.scrollIntoView();
      }), X(this.cm, R(i)), this.options.closeOnPick && this.close();
    },
    cursorActivity: function() {
      this.debounce && (J(this.debounce), this.debounce = 0);
      var t = this.startPos;
      this.data && (t = this.data.from);
      var e = this.cm.getCursor(), i = this.cm.getLine(e.line);
      if (e.line != this.startPos.line || i.length - e.ch != this.startLen - this.startPos.ch || e.ch < t.ch || this.cm.somethingSelected() || !e.ch || this.options.closeCharacters.test(i.charAt(e.ch - 1)))
        this.close();
      else {
        var n = this;
        this.debounce = G(function() {
          n.update();
        }), this.widget && this.widget.disable();
      }
    },
    update: function(t) {
      if (this.tick != null) {
        var e = this, i = ++this.tick;
        B(this.options.hint, this.cm, this.options, function(n) {
          e.tick == i && e.finishUpdate(n, t);
        });
      }
    },
    finishUpdate: function(t, e) {
      this.data && r.signal(this.data, "update");
      var i = this.widget && this.widget.picked || e && this.options.completeSingle;
      this.widget && this.widget.close(), this.data = t, t && t.list.length && (i && t.list.length == 1 ? this.pick(t, 0) : (this.widget = new W(this, t), r.signal(t, "shown")));
    }
  };
  function Q(t, e, i) {
    var n = t.options.hintOptions, o = {};
    for (var l in K)
      o[l] = K[l];
    if (n)
      for (var l in n)
        n[l] !== void 0 && (o[l] = n[l]);
    if (i)
      for (var l in i)
        i[l] !== void 0 && (o[l] = i[l]);
    return o.hint.resolve && (o.hint = o.hint.resolve(t, e)), o;
  }
  function $(t) {
    return typeof t == "string" ? t : t.text;
  }
  function j(t, e) {
    var i = {
      Up: function() {
        e.moveFocus(-1);
      },
      Down: function() {
        e.moveFocus(1);
      },
      PageUp: function() {
        e.moveFocus(-e.menuSize() + 1, !0);
      },
      PageDown: function() {
        e.moveFocus(e.menuSize() - 1, !0);
      },
      Home: function() {
        e.setFocus(0);
      },
      End: function() {
        e.setFocus(e.length - 1);
      },
      Enter: e.pick,
      Tab: e.pick,
      Esc: e.close
    }, n = /Mac/.test(navigator.platform);
    n && (i["Ctrl-P"] = function() {
      e.moveFocus(-1);
    }, i["Ctrl-N"] = function() {
      e.moveFocus(1);
    });
    var o = t.options.customKeys, l = o ? {} : i;
    function s(a, c) {
      var h;
      typeof c != "string" ? h = function(p) {
        return c(p, e);
      } : i.hasOwnProperty(c) ? h = i[c] : h = c, l[a] = h;
    }
    if (o)
      for (var u in o)
        o.hasOwnProperty(u) && s(u, o[u]);
    var f = t.options.extraKeys;
    if (f)
      for (var u in f)
        f.hasOwnProperty(u) && s(u, f[u]);
    return l;
  }
  function L(t, e) {
    for (; e && e != t; ) {
      if (e.nodeName.toUpperCase() === "LI" && e.parentNode == t)
        return e;
      e = e.parentNode;
    }
  }
  function W(t, e) {
    this.id = "cm-complete-" + Math.floor(Math.random(1e6)), this.completion = t, this.data = e, this.picked = !1;
    var i = this, n = t.cm, o = n.getInputField().ownerDocument, l = o.defaultView || o.parentWindow, s = this.hints = o.createElement("ul");
    s.setAttribute("role", "listbox"), s.setAttribute("aria-expanded", "true"), s.id = this.id;
    var u = t.cm.options.theme;
    s.className = "CodeMirror-hints " + u, this.selectedHint = e.selectedHint || 0;
    for (var f = e.list, a = 0; a < f.length; ++a) {
      var c = s.appendChild(o.createElement("li")), h = f[a], p = Y + (a != this.selectedHint ? "" : " " + O);
      h.className != null && (p = h.className + " " + p), c.className = p, a == this.selectedHint && c.setAttribute("aria-selected", "true"), c.id = this.id + "-" + a, c.setAttribute("role", "option"), h.render ? h.render(c, e, h) : c.appendChild(o.createTextNode(h.displayText || $(h))), c.hintId = a;
    }
    var g = t.options.container || o.body, v = n.cursorCoords(t.options.alignWithWord ? e.from : null), w = v.left, H = v.bottom, U = !0, b = 0, S = 0;
    if (g !== o.body) {
      var et = ["absolute", "relative", "fixed"].indexOf(l.getComputedStyle(g).position) !== -1, F = et ? g : g.offsetParent, z = F.getBoundingClientRect(), C = o.body.getBoundingClientRect();
      b = z.left - C.left - F.scrollLeft, S = z.top - C.top - F.scrollTop;
    }
    s.style.left = w - b + "px", s.style.top = H - S + "px";
    var T = l.innerWidth || Math.max(o.body.offsetWidth, o.documentElement.offsetWidth), E = l.innerHeight || Math.max(o.body.offsetHeight, o.documentElement.offsetHeight);
    g.appendChild(s), n.getInputField().setAttribute("aria-autocomplete", "list"), n.getInputField().setAttribute("aria-owns", this.id), n.getInputField().setAttribute("aria-activedescendant", this.id + "-" + this.selectedHint);
    var y = t.options.moveOnOverlap ? s.getBoundingClientRect() : new DOMRect(), D = t.options.paddingForScrollbar ? s.scrollHeight > s.clientHeight + 1 : !1, A;
    setTimeout(function() {
      A = n.getScrollInfo();
    });
    var it = y.bottom - E;
    if (it > 0) {
      var N = y.bottom - y.top, nt = v.top - (v.bottom - y.top);
      if (nt - N > 0)
        s.style.top = (H = v.top - N - S) + "px", U = !1;
      else if (N > E) {
        s.style.height = E - 5 + "px", s.style.top = (H = v.bottom - y.top - S) + "px";
        var _ = n.getCursor();
        e.from.ch != _.ch && (v = n.cursorCoords(_), s.style.left = (w = v.left - b) + "px", y = s.getBoundingClientRect());
      }
    }
    var k = y.right - T;
    if (D && (k += n.display.nativeBarWidth), k > 0 && (y.right - y.left > T && (s.style.width = T - 5 + "px", k -= y.right - y.left - T), s.style.left = (w = v.left - k - b) + "px"), D)
      for (var x = s.firstChild; x; x = x.nextSibling)
        x.style.paddingRight = n.display.nativeBarWidth + "px";
    if (n.addKeyMap(this.keyMap = j(t, {
      moveFocus: function(m, d) {
        i.changeActive(i.selectedHint + m, d);
      },
      setFocus: function(m) {
        i.changeActive(m);
      },
      menuSize: function() {
        return i.screenAmount();
      },
      length: f.length,
      close: function() {
        t.close();
      },
      pick: function() {
        i.pick();
      },
      data: e
    })), t.options.closeOnUnfocus) {
      var Z;
      n.on("blur", this.onBlur = function() {
        Z = setTimeout(function() {
          t.close();
        }, 100);
      }), n.on("focus", this.onFocus = function() {
        clearTimeout(Z);
      });
    }
    n.on("scroll", this.onScroll = function() {
      var m = n.getScrollInfo(), d = n.getWrapperElement().getBoundingClientRect();
      A || (A = n.getScrollInfo());
      var q = H + A.top - m.top, P = q - (l.pageYOffset || (o.documentElement || o.body).scrollTop);
      if (U || (P += s.offsetHeight), P <= d.top || P >= d.bottom)
        return t.close();
      s.style.top = q + "px", s.style.left = w + A.left - m.left + "px";
    }), r.on(s, "dblclick", function(m) {
      var d = L(s, m.target || m.srcElement);
      d && d.hintId != null && (i.changeActive(d.hintId), i.pick());
    }), r.on(s, "click", function(m) {
      var d = L(s, m.target || m.srcElement);
      d && d.hintId != null && (i.changeActive(d.hintId), t.options.completeOnSingleClick && i.pick());
    }), r.on(s, "mousedown", function() {
      setTimeout(function() {
        n.focus();
      }, 20);
    });
    var V = this.getSelectedHintRange();
    return (V.from !== 0 || V.to !== 0) && this.scrollToActive(), r.signal(e, "select", f[this.selectedHint], s.childNodes[this.selectedHint]), !0;
  }
  W.prototype = {
    close: function() {
      if (this.completion.widget == this) {
        this.completion.widget = null, this.hints.parentNode && this.hints.parentNode.removeChild(this.hints), this.completion.cm.removeKeyMap(this.keyMap);
        var t = this.completion.cm.getInputField();
        t.removeAttribute("aria-activedescendant"), t.removeAttribute("aria-owns");
        var e = this.completion.cm;
        this.completion.options.closeOnUnfocus && (e.off("blur", this.onBlur), e.off("focus", this.onFocus)), e.off("scroll", this.onScroll);
      }
    },
    disable: function() {
      this.completion.cm.removeKeyMap(this.keyMap);
      var t = this;
      this.keyMap = { Enter: function() {
        t.picked = !0;
      } }, this.completion.cm.addKeyMap(this.keyMap);
    },
    pick: function() {
      this.completion.pick(this.data, this.selectedHint);
    },
    changeActive: function(t, e) {
      if (t >= this.data.list.length ? t = e ? this.data.list.length - 1 : 0 : t < 0 && (t = e ? 0 : this.data.list.length - 1), this.selectedHint != t) {
        var i = this.hints.childNodes[this.selectedHint];
        i && (i.className = i.className.replace(" " + O, ""), i.removeAttribute("aria-selected")), i = this.hints.childNodes[this.selectedHint = t], i.className += " " + O, i.setAttribute("aria-selected", "true"), this.completion.cm.getInputField().setAttribute("aria-activedescendant", i.id), this.scrollToActive(), r.signal(this.data, "select", this.data.list[this.selectedHint], i);
      }
    },
    scrollToActive: function() {
      var t = this.getSelectedHintRange(), e = this.hints.childNodes[t.from], i = this.hints.childNodes[t.to], n = this.hints.firstChild;
      e.offsetTop < this.hints.scrollTop ? this.hints.scrollTop = e.offsetTop - n.offsetTop : i.offsetTop + i.offsetHeight > this.hints.scrollTop + this.hints.clientHeight && (this.hints.scrollTop = i.offsetTop + i.offsetHeight - this.hints.clientHeight + n.offsetTop);
    },
    screenAmount: function() {
      return Math.floor(this.hints.clientHeight / this.hints.firstChild.offsetHeight) || 1;
    },
    getSelectedHintRange: function() {
      var t = this.completion.options.scrollMargin || 0;
      return {
        from: Math.max(0, this.selectedHint - t),
        to: Math.min(this.data.list.length - 1, this.selectedHint + t)
      };
    }
  };
  function M(t, e) {
    if (!t.somethingSelected())
      return e;
    for (var i = [], n = 0; n < e.length; n++)
      e[n].supportsSelection && i.push(e[n]);
    return i;
  }
  function B(t, e, i, n) {
    if (t.async)
      t(e, n, i);
    else {
      var o = t(e, i);
      o && o.then ? o.then(n) : n(o);
    }
  }
  function tt(t, e) {
    var i = t.getHelpers(e, "hint"), n;
    if (i.length) {
      var o = function(l, s, u) {
        var f = M(l, i);
        function a(c) {
          if (c == f.length)
            return s(null);
          B(f[c], l, u, function(h) {
            h && h.list.length > 0 ? s(h) : a(c + 1);
          });
        }
        a(0);
      };
      return o.async = !0, o.supportsSelection = !0, o;
    } else
      return (n = t.getHelper(t.getCursor(), "hintWords")) ? function(l) {
        return r.hint.fromList(l, { words: n });
      } : r.hint.anyword ? function(l, s) {
        return r.hint.anyword(l, s);
      } : function() {
      };
  }
  r.registerHelper("hint", "auto", {
    resolve: tt
  }), r.registerHelper("hint", "fromList", function(t, e) {
    var i = t.getCursor(), n = t.getTokenAt(i), o, l = r.Pos(i.line, n.start), s = i;
    n.start < i.ch && /\w/.test(n.string.charAt(i.ch - n.start - 1)) ? o = n.string.substr(0, i.ch - n.start) : (o = "", l = i);
    for (var u = [], f = 0; f < e.words.length; f++) {
      var a = e.words[f];
      a.slice(0, o.length) == o && u.push(a);
    }
    if (u.length)
      return { list: u, from: l, to: s };
  }), r.commands.autocomplete = r.showHint;
  var K = {
    hint: r.hint.auto,
    completeSingle: !0,
    alignWithWord: !0,
    closeCharacters: /[\s()\[\]{};:>,]/,
    closeOnPick: !0,
    closeOnUnfocus: !0,
    updateOnCursorActivity: !0,
    completeOnSingleClick: !0,
    container: null,
    customKeys: null,
    extraKeys: null,
    paddingForScrollbar: !0,
    moveOnOverlap: !0
  };
  r.defineOption("hintOptions", null);
});
